package com.tongshu.multiplefiles.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tongshu.multiplefiles.config.UploadConfig;
import com.tongshu.multiplefiles.mapper.MyFileMapper;
import com.tongshu.multiplefiles.mapper.TempFileMapper;
import com.tongshu.multiplefiles.model.MyFile;
import com.tongshu.multiplefiles.model.TempFile;
import com.tongshu.multiplefiles.utils.FileUtils;
import com.tongshu.multiplefiles.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * 文件上传
 */
@Service
public class FileService {

    @Autowired
    private MyFileMapper myFileMapper;
    @Autowired
    private TempFileMapper tempFileMapper;

    public void uploadFile(String name, String md5, MultipartFile file) throws IOException {
        String path = UploadConfig.filePath + name;
        FileUtils.writeFile(path, file.getInputStream());
        myFileMapper.insert(new MyFile(name, path, md5, new Date(), 0));
    }

    public int isUpload(String md5) {
        // 第一步，在File表查询是否已有此记录
        QueryWrapper<MyFile> wrapper = new QueryWrapper();
        wrapper.eq("md5", md5);
        MyFile file = myFileMapper.selectOne(wrapper);
        if (file == null) {
            return 0; // 文件未上传
        } else {
            if (file.getStatus() == 1) {
                return 1; // 文件已上传完成
            }

            String fileMd5 = file.getMd5();
            // 第二步，在TempFile表查询是否有此记录
            QueryWrapper<TempFile> tempFileQueryWrapper = new QueryWrapper<>();
            tempFileQueryWrapper.eq("fileMd5", fileMd5);
            List<TempFile> tempFiles = tempFileMapper.selectList(tempFileQueryWrapper);
            if (tempFiles != null && tempFiles.size() != 0) {
                // 已上传部分，转为断点续传
                return 2; // 已上传部分
            }
            return 0; // 文件未上传
        }
    }

    public Map<String, Object> isUpload1(String md5, Integer chunkNum) {
        Map<String, Object> map = new HashMap<>();
        if (!UploadUtils.isExist(md5)) {
            map.put("flag", 0); // 文件未上传
            return map;
        }
        if (UploadUtils.isUploaded(md5)) {
            map.put("flag", 1); // 已上传完成
            return map;
        }
        // 获取未上传分块索引队列
        List<Integer> unUploaded = new ArrayList<>();
        unUploaded = UploadUtils.disUploaded(md5);
        if (unUploaded.size() == chunkNum) {
            map.put("flag", 0); // 文件未上传
            return map;
        }
        map.put("flag", 2);
        map.put("unUploadedList", unUploaded);
        return map;

    }

    // 保存分块记录到数据库表
    public void saveTempChunk(String fileMd5, String chunkMd5, Date uploadTime, int status, int index) {
        tempFileMapper.insert(new TempFile(fileMd5, chunkMd5, uploadTime, status, index));
    }

    // 保存分块记录到数据库表
    public void saveMyFile(String filename, String path, String md5, Date uploadTime, int status) {
        myFileMapper.insert(new MyFile(filename, path, md5, uploadTime, status));
    }


}
